package org.paradise.dms.web.action.buildingsmgr;

import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.Apartment;
import org.paradise.dms.pojo.Dormitory;
import org.paradise.dms.services.ApartmentService;
import org.paradise.dms.services.BuildingsMgrService;
import org.paradise.dms.services.DormitoryService;
import org.paradise.dms.util.JsonUtil;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 宿舍楼宇管理(CRUD)
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-30 上午01:19:26 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "buildingsMgrAction")
public class BuildingsMgrAction extends DMSBaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(BuildingsMgrAction.class);
	@Autowired
	private BuildingsMgrService buildingsMgrService;
	@Autowired
	private ApartmentService apartmentService;
	@Autowired
	private DormitoryService dormitoryService;

	public DormitoryService getDormitoryService() {
		return dormitoryService;
	}

	public void setDormitoryService(DormitoryService dormitoryService) {
		this.dormitoryService = dormitoryService;
	}

	public ApartmentService getApartmentService() {
		return apartmentService;
	}

	public void setApartmentService(ApartmentService apartmentService) {
		this.apartmentService = apartmentService;
	}

	@Autowired
	private PagerService pagerService;
	private Pager pager;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;

	/**
	 * Description: 增加一条公寓信息
	 * 
	 * @Version1.0 2009-3-31 上午02:45:59 闫克 （kurtyan777@gmail.com）创建
	 * @param apartmentname
	 * @param apartmentfloornum
	 * @param apartmentmgrname
	 * @param apartmenttelephone
	 * @return
	 * 
	 */
	public String insertApartment(String apartmentname, String apartmentfloornum, String apartmentmgrname, String apartmenttelephone) {
		Apartment apartment = new Apartment();
		apartment.setApartmentname(apartmentname);
		apartment.setApartmentfloornum(apartmentfloornum);
		apartment.setApartmentmgrname(apartmentmgrname);
		apartment.setApartmenttelephone(apartmenttelephone);
		apartment.setApartmentind("1");
		apartment.setApartmenttotalrooms("0");
		return this.buildingsMgrService.insertApartment(apartment);
	}

	/**
	 * 
	 * Description: 分页获取所有的公寓
	 * 
	 * @Version1.0 2009-3-30 上午01:32:45 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public JSONArray ListAllApartmentListByPage(int pageNum, int totalRow, HttpSession session) {

		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 5);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 5;
		int ps = page.getPageSize();
		List<Apartment> lst = apartmentService.listAllApartmentByPage(ps, cp);
		if (lst != null & lst.size() > 0) {
			JSONArray ja = JSONArray.fromObject(lst);
			return ja;
		}
		return null;

	}

	/**
	 * 
	 * Description: 获取公寓总行数记录
	 * 
	 * @Version1.0 Apr 16, 2009 1:11:04 PM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public int getAprtTotalRows() {
		return apartmentService.getAllApartmentRows();
	}

	/**
	 * 
	 * Description: 更新一条公寓信息
	 * 
	 * @Version1.0 2009-3-30 上午11:58:41 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public String updateApartment(String apartmentStr) {
		// apt.apartmentid,apt.apartmentname,apt.apartmentfloornum,apt.apartmentmgrname,apt.apartmenttelephone
		// apt.apartmenttotalrooms,apt.apartmentavailrooms,apt.apartmentind
		Apartment apartment = new Apartment();
		String[] apartstr = apartmentStr.split(",");
		// for (int i = 0; i < apartstr.length; i++) {
		// log.info(apartstr[i]);
		// }
		apartment.setApartmentid(apartstr[0]);
		apartment.setApartmentname(apartstr[1]);
		apartment.setApartmentfloornum(apartstr[2]);
		if ("".equals(apartstr[3])) {
			apartment.setApartmentmgrname(null);
		} else {
			apartment.setApartmentmgrname(apartstr[3]);
		}

		if ("".equals(apartstr[4])) {
			apartment.setApartmenttelephone(null);
		} else {
			apartment.setApartmenttelephone(apartstr[4]);
		}
		if ("".equals(apartstr[5])) {
			apartment.setApartmenttotalrooms(null);
		} else {
			apartment.setApartmenttotalrooms(apartstr[5]);
		}
		if ("".equals(apartstr[6])) {
			apartment.setApartmentavailrooms(null);
		} else {
			apartment.setApartmentavailrooms(apartstr[6]);
		}
		apartment.setApartmentind(apartstr[7]);
		return apartmentService.updateApartment(apartment);
	}

	/**
	 * 更新一条宿舍信息
	 * @param dormitoryStr
	 * @return
	 */
	public String updateDormitory(JSONObject dormitoryJSONObject) {
		log.info("宿舍信息是:"+dormitoryJSONObject);
		Dormitory dormitory = (Dormitory)JSONObject.toBean(dormitoryJSONObject, Dormitory.class);
		return dormitoryService.updateDormitory(dormitory);
	}

	/**
	 * 
	 * Description: 根据apartmentid，删除一条公寓信息，返回删除状态
	 * 
	 * @Version1.0 2009-3-30 下午08:51:23 李双江（paradise.lsj@gmail.com）创建
	 * @param apartmentid
	 * @return
	 */
	public String delelteApartmentByID(String apartmentid) {
		return apartmentService.deleteApartmentByApartmentID(apartmentid);
	}

	/**
	 * 
	 * Description: 根据宿舍id删除一条宿舍
	 * 
	 * @Version1.0 2009-3-30 下午10:55:52 李双江（paradise.lsj@gmail.com）创建
	 * @param dormitory
	 * @return
	 */
	public String delelteDormitoryByID(String dormitoryid) {
		return dormitoryService.deleteDormitoryByDormitoryID(dormitoryid);
	}

	/**
	 * 
	 * Description: 分页查询指定了apartmentid的宿舍
	 * 
	 * @Version1.0 2009-3-30 下午10:08:19 李双江（paradise.lsj@gmail.com）创建
	 * @Version1.0 2009-4-20 下午15:38:19 李双江（paradise.lsj@gmail.com）修改增加floorno
	 * @param pageNum
	 * @param totalRow
	 * @param apartmentid
	 * @param floorno
	 * @param session
	 * @return
	 */
	@SuppressWarnings( { "static-access", "unchecked" })
	public JSONArray ListAllDormListPerPageByApartmentIDAndFloorNo(int pageNum, int totalRow, String apartmentid, String floorno, HttpSession session) {
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 12);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 12;
		int ps = page.getPageSize();
		List list = dormitoryService.listDormitoryByApartmentIDAndFloorNo(apartmentid, floorno, ps, cp);
		if (list != null & list.size() > 0) {
			JSONArray dlist = new JSONArray().fromObject(list);
			return dlist;
		}
		return null;
	}

	/**
	 * 
	 * Description: 分页查询指定了apartmentid的宿舍
	 * 
	 * @Version1.0 2009-3-30 下午10:08:19 李双江（paradise.lsj@gmail.com）创建
	 * @param pageNum
	 * @param totalRow
	 * @param apartmentid
	 * @param floorno
	 * @param session
	 * @return
	 */
	@SuppressWarnings( { "static-access", "unchecked" })
	public JSONArray ListAllDormListPerPageByApartmentID(String sql) {
		List list = dormitoryService.listDormitoryByApartmentID(sql);
		if (list != null & list.size() > 0) {
			JSONArray dlist = new JSONArray().fromObject(list);
			return dlist;
		}
		return null;
	}

	/**
	 * 
	 * Description:返回DWR页面分页对象
	 * 
	 * @Version1.0 2009-3-28 上午11:13:26 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session.getAttribute("page"));
		return joa;
	}

	/**
	 * 
	 * Description: 根据apartmentid查询公寓的总宿舍行数
	 * 
	 * @Version1.0 2009-3-30 下午09:54:23 李双江（paradise.lsj@gmail.com）创建
	 * @Version1.0 2009-4-20 下午15:34:23 李双江（paradise.lsj@gmail.com）修改增加了floorno
	 * @param apartmentid
	 * @return
	 * @throws DaoException
	 */
	public int getApartmentDormTotalRows(String apartmentid, String floorno) throws DaoException {
		return dormitoryService.getRows(apartmentid, floorno);
	}

	/**
	 * 
	 * Description: 根据apartmentid查询公寓的总宿舍行数
	 * 
	 * @Version1.0 2009-3-30 下午09:54:23 李双江（paradise.lsj@gmail.com）创建
	 * @param apartmentid
	 * @return
	 * @throws DaoException
	 */
	public int getApartmentDormTotalRowsByID(String apartmentid) throws DaoException {
		return dormitoryService.getRows(apartmentid);
	}

	/**
	 * 
	 * Description:根据模板批量更新宿舍信息
	 * 
	 * @Version1.0 2009-4-2 上午04:20:48 李双江（paradise.lsj@gmail.com）创建
	 * @param jsonString
	 * @return
	 * @throws DaoException
	 */
	public String updateBatchDormitoryInfo(String jArray) throws DaoException {
		return buildingsMgrService.insertDormitoryInfo(jArray);
	}

	/**
	 * 
	 * Description: 根据传入的dormitoryid的列表和预分配的id值批量更新宿舍为某个预分配标记
	 * 
	 * @Version1.0 2009-4-6 下午01:49:11 李双江（paradise.lsj@gmail.com）创建
	 * @param dormidList
	 * @param dormitoryallocationid
	 * @return
	 */
	public String updateBatchDormitoryPreassignInfo(String dormidList, String dormitoryallocationid) {
		return dormitoryService.updateBatchDormitoryPreassignInfo(dormidList, dormitoryallocationid);
	}

	/**
	 * 
	 * Description: 宿舍批量更新页面批量标记整个公寓的preid方法
	 * 
	 * @Version1.0 Apr 29, 2009 12:24:53 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param apartmentid
	 * @param dormallocationid
	 * @return
	 */

	public String updateAApartmentDormPreID(String apartmentid, String dormallocationid) {
		return dormitoryService.updateAApartmentDormPreID(apartmentid, dormallocationid);
	}

	public BuildingsMgrService getBuildingsMgrService() {
		return buildingsMgrService;
	}

	public void setBuildingsMgrService(BuildingsMgrService buildingsMgrService) {
		this.buildingsMgrService = buildingsMgrService;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
}
